﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Wedn.Core.Entity;
using Wedn.Core.Service;
using Wedn.Core.Utility;

namespace Wedn.Web.UI
{
    public class AdminPage : PageBase
    {
        public User CurrentUser { get; set; }
        protected override void OnInit(EventArgs e)
        {
            CurrentUser = Session["CurrentUser"] as User;
            if (CurrentUser == null)
            {
                //检查cookie
                CheckCookie();
            }
            base.OnInit(e);
        }
        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);
        //}

        /// <summary>
        /// 校验cookie登录
        /// </summary>
        private void CheckCookie()
        {
            HttpCookie cookie = Request.Cookies[Config.SitePrefix + "URM"];
            if (cookie != null)
            {
                UserManager userManager = new UserManager();
                User cookieUser = userManager.GetDecryptUserInfo(cookie.Value);
                if (cookieUser!=null)
                {
                    User currentUser = null;
                    LoginResult res = userManager.Login(cookieUser.Login, cookieUser.Password, out currentUser, true);
                    if (res == LoginResult.Success)
                    {
                        //审核通过
                        if (Session["CurrentUser"] == null)
                            Session["CurrentUser"] = currentUser;
                    }
                }
                else
                {
                    //密码被修改了.清除cookie
                    Response.Cookies[Config.SitePrefix + "URM"].Expires = DateTime.Now.AddDays(-1);
                    Redirect();
                } 
            }
            else
            {
                Redirect();
            }
        }

        /// <summary>
        /// 页面重定向到登录界面
        /// </summary>
        private void Redirect()
        {
            Response.Redirect("/admin/login.html?redirectUrl=" + Request.Url.ToString());
        }
        /// <summary>
        /// 分页HTML
        /// </summary>
        public string Paged(int count, int pageSize)
        {
            int pageIndex = RequestHelper.QueryInt("page", 1);
            int pageCount = (int)Math.Ceiling((double)count / pageSize);
            StringBuilder sbd = new StringBuilder();
            sbd.AppendFormat("<span class=\"displaying-num\">{0} 个项目</span>\r\n", count);
            if (pageIndex != 1)
            {
                sbd.AppendFormat("<a title=\"前往第一页\" href=\"?page=1\">«</a>");
                sbd.AppendFormat("<a title=\"前往上一页\" href=\"?page={0}\">‹</a>", pageIndex - 1);
            }
            sbd.AppendFormat("<span>第<input class=\"inpt-page\" title=\"当前页面\" type=\"text\" name=\"paged\" value=\"{0}\"/>页，共 <span>{1}</span> 页</span>", pageIndex, pageCount);
            if (pageIndex != pageCount)
            {
                sbd.AppendFormat("<a title=\"前往下一页\" href=\"?page={0}\">›</a>", pageIndex + 1);
                sbd.AppendFormat("<a title=\"前往最后一页\" href=\"?page={0}\">»</a>", pageCount);
            }
            return sbd.ToString();
        }
    }
}
